package com.cangmaomao.m_mePen.fragment

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import com.cangmaomao.lib.base.BaseFragment
import com.cangmaomao.lib.utils.MyCountTimerUtils
import com.cangmaomao.lib.utils.toast
import com.cangmaomao.m_mePen.R
import com.cangmaomao.m_mePen.contract.RegContract
import com.cangmaomao.m_mePen.presenter.RegPresenter
import kotlinx.android.synthetic.main.f_reg.*

/**
 * 注册
 */
class RegFragment : BaseFragment<RegContract.Presenter>(), RegContract.View {


    private lateinit var timerUtils: MyCountTimerUtils

    override fun context(): Context = _mActivity
    override val contentViewLayoutID: Int
        get() = R.layout.f_reg

    override fun initView(savedInstanceState: Bundle?) {
        initToolBarNav(getString(R.string.reg), getString(R.string.sure))
        RegPresenter(this)
        b_code.setOnClickListener {
            try {
                p.getVerificationCode()
                timerUtils = MyCountTimerUtils(b_code, getString(R.string.obtain), 60, 1)
                timerUtils.start()
            } catch (e: Exception) {
                toast(e.message!!)
            }
        }
    }

    override fun password(): String {
        val pwd = pwd.text.toString()
        if (!TextUtils.isEmpty(pwd)) return pwd else throw Exception("密码不能为空!")
    }

    override fun code(): String {
        val verificationCode = verificationCode.text.toString()
        if (!TextUtils.isEmpty(verificationCode)) return verificationCode else throw Exception("验证码不能为空!")
    }

    override fun phone(): String {
        val account = account.text.toString()
        if (!TextUtils.isEmpty(account)) return account else throw Exception("手机号码不能为空!")
    }

    override fun loadMap(): Map<String, String> {
        return mapOf(
                "mId" to "123456789",
                "name" to et_name.text.toString(),
                "mobile" to phone(),
                "password" to password(),
                "verificationCode" to code())
    }

    override fun responseData(flag: Boolean, msg: String) {
        if (flag) {
            toast(msg)
            pop()
        } else {
            toast(msg)
        }
    }

    override fun requestFail(it: Throwable) {

    }

    override fun onRightClick() {
        try {
            p.start()
        } catch (e: Exception) {
            toast(e.message!!)
        }
    }
}